<?php
namespace Manage\Controller;
use Manage\Controller\BaseController;
use Think\Controller;
class WeixinController extends BaseController {

    public function index(){

    }

    /**
     * @param $access_token
     * $barcodeArr 数组
     * @return mixed
     * 生成带参二维码
     */
    public function twoDBarcode(){
        $obj = M('b2c_2d_barcode');
        $this->_arr['p'] = I('p',1,'intval');
        $this->_arr['select'] = I('select');
        $this->_arr['value'] = trim(I('value'));
        $where = '1=1';
        if($this->_arr['select'] && $this->_arr['value']){
            switch($this->_arr['select']){
                case 'id' :
                    $where .= ' and b.barcode_id = '.$this->_arr['value'];
                    break;
                case 'barcode_name' :
                    $where .= " and b.barcode_name like '".$this->_arr['value']."%'";
                    break;
                case 'store_name' :
                    //$local_data = M('webpos_local_store')->getFieldByLocal_name($this->_arr['value'],'local_id');
                    $local_data = M('webpos_local_store')->where(['local_name'=>['like',"%{$this->_arr['value']}%"]])->getField('local_id',true);
                    //echo M()->_sql();
                    //dump($local_data);die;
                    $where .= ' and b.local_id in ('.implode(',',$local_data).')';
                    break;
            }
        }

        $order_id = I('id') ? I('id') : 2;
        if($order_id == 1){
            $order = 'barcode_id asc';
            $this->_arr['rank'] = 2;
        }elseif($order_id == 2){
            $order = 'barcode_id desc';
            $this->_arr['rank'] = 1;
        }
        $data = $obj->alias('b')->field('b.*,s.local_name')->join("sdb_webpos_local_store s ON s.local_id = b.local_id", 'left')
            ->where($where)->order($order)->page($this->_arr['p'].','.$this->page_num)->select();
        //echo M()->_sql();
        $count      = $obj->where($where)->count();// 查询满足要求的总记录数
        $Page       = new \Think\Page($count,$this->page_num);// 实例化分页类 传入总记录数和每页显示的记录数
        $show       = $Page->show();// 分页显示输出
        $this->_arr['page'] = $show;// 赋值分页输出
        $this->_arr['list'] = $data;
        $this->_showDisplay();
    }

    /**
     * 新增
     */
    public function newTwoDBarcode(){
        $store = M('webpos_local_store')->getField('local_id,local_name');
        $this->_arr['store'] = $store;
        $this->_showDisplay();
    }
    public function doNewTwoDBarcode(){
        $code_obj = M('b2c_2d_barcode');
        $barcode_name = I('barcode_name') ? I('barcode_name') : $this->error('门店名称不能为空！');
        $b_parameter = I('b_parameter') ? I('b_parameter') : $this->error('参数不能为空！');
        $local_id = I('local_id') ? I('local_id') : $this->error('门店ID不能为空！');
        //$b_parameter = '100'.$b_parameter;
        $remark = I('remark');
        $barcodeArr = array(
            'barcode_name' => $barcode_name,
            'b_parameter' => $b_parameter,
            'local_id' => $local_id,
            'remark' => $remark,
        );
        //查询已有参数是否已存在
        $code_c = $code_obj->where(['b_parameter'=>$b_parameter])->find();
        if($code_c){
            $this->error('该参数已使用，请更换！');
        }
        $obj = new \Home\Event\WeixinEvent();
        $data = $obj->twoDBarcode($barcodeArr);
        if(isset($data['errcode'])){
            return $data;
        }
        $barcodeArr['barcode_img'] = $data['ticket'];
        $barcodeArr['regtime'] = time();
        $stat = $code_obj->data($barcodeArr)->add();
        $stat === false ? $this->error('配置失败','twoDBarcode') : $this->success('配置成功', 'twoDBarcode');
    }
    /**
     * 修改
     */
    public function saveTwoDBarcode(){
        $id = I('id') ? I('id') : $this->error('参数为空！');
        if(!is_numeric($id)) $this->error('无效参数！');
        $obj = M('b2c_2d_barcode');
        $data = $obj->where(['barcode_id'=>$id])->find();
        $store = M('webpos_local_store')->getField('local_id,local_name');
        $this->_arr['data'] = $data;
        $this->_arr['store'] = $store;
        $this->_showDisplay();
    }
    public function doSaveTwoDBarcode(){
        $code_obj = M('b2c_2d_barcode');
        $id = I('id') ? I('id') : $this->error('参数为空！');
        if(!is_numeric($id)) $this->error('无效参数！');
        $barcode_name = I('barcode_name') ? I('barcode_name') : $this->error('门店名称不能为空！');
        $local_id = I('local_id') ? I('local_id') : $this->error('门店ID不能为空！');
        $remark = I('remark');
        $barcodeArr = array(
            'barcode_name' => $barcode_name,
            'local_id' => $local_id,
            'remark' => $remark,
        );
        $stat = $code_obj->where([['barcode_id'=>$id]])->data($barcodeArr)->save();
        $stat === false ? $this->error('配置失败','twoDBarcode') : $this->success('配置成功', 'twoDBarcode');
    }

    /**
     * 查看图片
     */
    public function lookTwoDBarcode(){
        $this->_arr['img'] = I('img');
        $this->_showDisplay();
    }

    /**
     * 模板消息列表
     */
    public function wxInformation(){
        $this->_arr['title_two'] = '微信模板列表';
        $this->_arr['p'] = I('p', 1, 'intval');
        $where = '1=1';
        if($this->_arr['title_name']){
            $where .= " and title_name like '%".$this->_arr['title_name']."%''";
        }
        $sl_id = I('id') ? I('id') : 2;
        if ($sl_id == 1) {
            $order = 'wx_id asc';
            $this->_arr['rank'] = 2;
        } elseif ($sl_id == 2) {
            $order = 'wx_id desc';
            $this->_arr['rank'] = 1;
        }
        //dump($where);die;
        $data = M('wx_information')
            ->where($where)
            ->order($order)
            ->page($this->_arr['p'] . ','.$this->page_num)
            ->select();
        //echo M()->_sql();
        $count = M('wx_information')->where($where)->count();// 查询满足要求的总记录数
        //dump($count);
        $Page = new \Think\Page($count, $this->page_num);// 实例化分页类 传入总记录数和每页显示的记录数
        $show = $Page->show();// 分页显示输出
        //dump($show);
        $this->_arr['page'] = $show;// 赋值分页输出
        $this->_arr['list'] = $data;
        $this->_showDisplay('Weixin/listIndex');
    }

    /**
     * 新增微信消息模板
     */
    public function newTemplate(){
        $this->_showDisplay();
    }
    public function doNewTemplate(){
        $obj = new \Home\Event\WeixinEvent();
        $data['number'] = I('number') or $this->error('编号不能为空');
        $data['title_name'] = I('title_name') or $this->error('模板标题不能为空');
        $data['is_terrace'] = I('is_terrace',0);
        $data['is_type'] = I('is_type',0);
        $data['create_time'] = time();
        //请求微信获取模板ID
        $acquire = $obj->acquireTemplate($data['number']);
        if(!$acquire['template_id']){
            $this->error('未请求到模板ID');
        }
        $data['template_id'] = $acquire['template_id'];
        $status = M('wx_information')->add($data);
        $status === false ? $this->error('失败') : $this->success('配置成功', 'wxInformation');
    }


  

    

   

}